namespace Maple.Branch.ConsoleGateway
{
    using Base.Consts;
    using Configurations;
    using Features;
    using Identity;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    using Permissions;
    using Tenants;
    using Volo.Abp;
    using Volo.Abp.AspNetCore.Authentication.JwtBearer;
    using Volo.Abp.AspNetCore.MultiTenancy;
    using Volo.Abp.Autofac;
    using Volo.Abp.Modularity;

    [DependsOn(typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
        typeof(AbpAspNetCoreMultiTenancyModule),
        typeof(FeaturesWebApiModule),
        typeof(IdentityWebApiModule),
        typeof(PermissionsWebApiModule),
        typeof(TenantsWebApiModule))]
    public class ConsoleWebHostModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            var rootConfiguration = new ConsoleConfiguration();

            configuration.GetSection("Console").Bind(rootConfiguration);

            context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = rootConfiguration.Authority;
                    options.ApiName = rootConfiguration.ApiName;
                    options.RequireHttpsMetadata = rootConfiguration.RequireHttpsMetadata;
                });

            // ocelot
            context.Services.AddOcelot(configuration.GetSection("Ocelot"));
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var configuration = context.GetConfiguration();

            app.UseCorrelationId();
            app.UseVirtualFiles();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseAbpClaimsMap();

            if (BranchConsts.IsMultiTenancyEnabled)
            {
                app.UseMultiTenancy();
            }

            app.UseAbpRequestLocalization();

            //app.UseSwagger();
            //app.UseSwaggerUI(options =>
            //{
            //    options.SwaggerEndpoint($"/swagger/v1/swagger.json", configuration["Swagger:Title"]);

            //    options.DocumentTitle = configuration["Swagger:Title"];

            //    options.DocExpansion(DocExpansion.None);
            //});

            app.MapWhen(
                ctx => ctx.Request.Path.ToString().StartsWith("/api/abp/") ||
                       ctx.Request.Path.ToString().StartsWith("/Abp/"),
                app2 =>
                {
                    app2.UseRouting();
                    app2.UseConfiguredEndpoints();
                }
            );

            app.UseConfiguredEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseOcelot().Wait();
        }
    }
}
